<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="UIWindowUIWindow是一种特殊的UIView，通常在一个app中只会有一个UIWindow，iOS程序启动完毕后，创建的第一个视图控件就是UIWindow（创建的第一个对象是UIapplication），接着创建控制器的view，最后将控制器的view添加到UIWindow上，于是控制器的view就显示在屏幕上了一个iOS程序之所以能显示到屏幕上，完全是因为它有UIWindow
也就说">
<meta property="og:type" content="article">
<meta property="og:title" content="UIWindow">
<meta property="og:url" content="https://github.com/frthinker/2016/11/01/UIWindow/index.html">
<meta property="og:site_name" content="lonely thinker">
<meta property="og:description" content="UIWindowUIWindow是一种特殊的UIView，通常在一个app中只会有一个UIWindow，iOS程序启动完毕后，创建的第一个视图控件就是UIWindow（创建的第一个对象是UIapplication），接着创建控制器的view，最后将控制器的view添加到UIWindow上，于是控制器的view就显示在屏幕上了一个iOS程序之所以能显示到屏幕上，完全是因为它有UIWindow
也就说">
<meta property="og:updated_time" content="2016-11-11T13:04:16.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="UIWindow">
<meta name="twitter:description" content="UIWindowUIWindow是一种特殊的UIView，通常在一个app中只会有一个UIWindow，iOS程序启动完毕后，创建的第一个视图控件就是UIWindow（创建的第一个对象是UIapplication），接着创建控制器的view，最后将控制器的view添加到UIWindow上，于是控制器的view就显示在屏幕上了一个iOS程序之所以能显示到屏幕上，完全是因为它有UIWindow
也就说">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://github.com/frthinker/2016/11/01/UIWindow/"/>





  <title> UIWindow | lonely thinker </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">lonely thinker</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">thinking in my own world</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="https://github.com/frthinker/2016/11/01/UIWindow/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="王飚">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="/images/avatar.gif">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="lonely thinker">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="lonely thinker" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                UIWindow
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time title="Post created" itemprop="dateCreated datePublished" datetime="2016-11-01T12:40:01+08:00">
              2016-11-01
            </time>

            &nbsp;|&nbsp;

            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-check-o"></i>
            </span>
            <time title="Post modified" itemprop="dateModified" datetime="2016-11-11T21:04:16+08:00">
              2016-11-11
            </time>
            
          </span>

          

          
            
          

          

          
          

          
          
          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="UIWindow"><a href="#UIWindow" class="headerlink" title="UIWindow"></a>UIWindow</h2><p>UIWindow是一种特殊的UIView，通常在一个app中只会有一个UIWindow，iOS程序启动完毕后，创建的<strong>第一个视图控件</strong>就是UIWindow（创建的第一个对象是UIapplication），接着创建控制器的view，最后将控制器的view添加到UIWindow上，于是控制器的view就显示在屏幕上了<br>一个iOS程序之所以能显示到屏幕上，完全是因为它有UIWindow</p>
<p>也就说，没有UIWindow，就看不见任何UI界面<br><code>![1.png](http://ogharu8ll.bkt.clouddn.com/1.png)</code></p>
<p>添加<br><code>![2.png](http://ogharu8ll.bkt.clouddn.com/2.png)</code></p>
<p>先创建UIwindow,再创建控制器，创建控制器的view，然后将控制器的view添加到UIWindow上。<br>文档的解释：</p>
<p><code>![3.png](http://ogharu8ll.bkt.clouddn.com/3.png)</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">// 让当前UIWindow变成keyWindow，并显示出来</div><div class="line">- (void)makeKeyAndVisible; </div><div class="line"></div><div class="line">// 获取当前应用程序的主窗口</div><div class="line">- [UIApplication sharedApplication].keyWindow</div><div class="line"></div><div class="line">// 获取view对象所在的窗口</div><div class="line">- view.window</div><div class="line"></div><div class="line">// 获取当前程序所有的window</div><div class="line">- [UIApplication sharedApplication].windows</div></pre></td></tr></table></figure>
<p>创建一个控制器，把view添加到uiwindow上面（有两种方式）</p>
<p>直接将控制器的view添加到UIWindow中，并不理会它对应的控制器</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[self.window  addsubview:vc.view];</div></pre></td></tr></table></figure>
<p>（2）设置uiwindow的根控制器，自动将rootviewcontroller的view添加到window中，负责管理rootviewcontroller的生命周期</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[self.window.rootviewcontroller=vc];</div></pre></td></tr></table></figure>
<p>两个方法的区别：</p>
<p>以后的开发中，建议使用（2）.因为方法（1）存在一些问题，比如说控制器上面可能由按钮，需要监听按钮的点击事件，如果是1，那么按钮的事件应该由控制器来进行管理。但控制器是一个局部变量，控制器此时已经不存在了，但是控制器的view还在，此时有可能会报错。注意：方法执行完，这个控制器就已经不存在了。</p>
<p>问题描述1：当view发生一些事件的时候，通知控制器，但是控制器已经销毁了，所以可能出现未知的错误。</p>
<p>问题描述2：添加一个开关按钮，让屏幕360度旋转（两者的效果不一样）。当发生屏幕旋转事件的时候，UIapplication对象会将旋转事件传递给uiwindow,uiwindow又会将旋转事件传递给它的根控制器，由根控制器决定是否需要旋转</p>
<p>UIapplication-&gt;uiwindow-&gt;根控制器（第一种方式没有根控制器，所以不能跟着旋转）。</p>
<p>提示：不通过控制器的view也可以做开发，但是在实际开发中，不要这么做，不要直接把view添加到UIWindow上面去。因为，难以管理。</p>
<h2 id="四大对象的关系"><a href="#四大对象的关系" class="headerlink" title="四大对象的关系"></a>四大对象的关系</h2><ul>
<li>程序第一个创建的对象是<code>UIApplication</code></li>
<li>UIApplication绑定一个<code>AppDelegate</code>对象</li>
<li>AppDelegate对象中有一个<code>window</code>属性(UIWindow)</li>
<li>UIWindow对象中有一个<code>rootViewController</code>的属性</li>
<li>rootViewController设置成要显示的UIViewController</li>
<li>最后 显示出UIViewController对象的view</li>
</ul>
<p><code>![4.png](http://ogharu8ll.bkt.clouddn.com/4.png)</code></p>
<ol>
<li>UIApplication :delegate属性</li>
<li>AppDelegate :window属性</li>
<li>UIWindow :rootViewController属性</li>
<li>UIViewController :view属性</li>
</ol>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/10/31/autoLayout/" rel="next" title="iOS自动布局">
                <i class="fa fa-chevron-left"></i> iOS自动布局
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/11/02/shell/" rel="prev" title="贝壳屋">
                贝壳屋 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.gif"
               alt="王飚" />
          <p class="site-author-name" itemprop="name">王飚</p>
          <p class="site-description motion-element" itemprop="description"></p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">4</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          

          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#UIWindow"><span class="nav-number">1.</span> <span class="nav-text">UIWindow</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#四大对象的关系"><span class="nav-number">2.</span> <span class="nav-text">四大对象的关系</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">王飚</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	




  
  

  

  

  

  


</body>
</html>
